<template>
  <div class="table">
    <div class="content">
      <div class="article_search">
        <div class="title">
          <el-input
            placeholder="文章搜索器"
            prefix-icon="el-icon-search"
            v-model.trim="select_word"
            @keyup.enter.native="search_key(select_word)"
          ></el-input>
          <el-button type="primary" icon="search" @click="search_key(select_word)">搜索</el-button>
        </div>
        <ul class="comment_list_left">
          <li v-for="(item,idx) in article_list" :key="idx" @click="enterList(item.news_id)" :class="{'bdcolor':colorId==item.news_id}">
            <h2>{{item.news_name}}</h2>
            <p>
              <span>{{item.apply_time}}</span>
              <span>{{item.comment_num}}条</span>
            </p>
          </li>
        </ul>
        <div class="Pagination_block">
          <el-pagination
            small
            :total="Totals"
            :page-count="pageTotal"
            :page-size="10"
            :current-page.sync="currPage"
            @current-change="handleCurrentChange"
            layout="prev, pager, next, jumper"
          ></el-pagination>
        </div>
      </div>
      <div class="list_right">
        <div class="title">
          <h2>文章评论</h2>
        </div>
        <div class="comment_table">
          <ul>
            <li v-for="item in comment_table" :key="item.comment_id" >
              <div class="left">
                <h2>{{item.nickname}}</h2>
                <div class="desc">
                  {{item.comment_txt}}
                </div>
                <p><span>{{item.add_time}}</span></p>
              </div>
              <div class="right">
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  circle
                  @click="delate(item.comment_id)"
                ></el-button>
              </div>
            </li>
          </ul>
        </div>
        <div class="comment_bottom">
          <el-pagination
            small
            :total="commenTotals"
            :page-count="commenPageTotal"
            :page-size="10"
            :current-page.sync="commentPage"
            @current-change="handComment"
            layout="prev, pager, next, jumper"
          ></el-pagination>
        </div>
      </div>
    </div>
    <!-- 删除提示框 -->
    <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
      <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteRow">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import * as api from "../../http/api.js";
export default {
  data() {
    return {
      select_word: "", //左侧 搜索关键字
      article_list: [], //文章列表
      comment_table: [], // 评论列表
      delVisible: false, //点击删除弹出的信息框状态默认为不显示
      Token: sessionStorage.getItem("Token"),
      currPage: 1, //分页页码
      Totals: 1, //总条数 默认为1条，在获取接口列表时在传递过来，改变1
      pageTotal: 2, //总页数
      news_id: "", //文章id 用来连接详情的参数
      commentPage: 1, // 下面的都是评论的分页属性了
      commenTotals: 1,
      commenPageTotal: 1,
      commenID: "",
      colorId:null,// 这个是颜色对应的id
      newsID:"",//文章id，评论接口
    };
  },
  methods: {
    getArticle(key) {
      this.$post(api.news_list, {
        token: this.Token,
        page: this.currPage,
        status: 1, //审核通过
        news: key //新闻标题
      })
        .then(res => {
          if (res.status == "0") {
            this.article_list = res.data;
            // 获取分页数据
            this.Totals = res.total;
            this.pageTotal = res.pageTotal;
            this.currPage = res.currPage;
            //点击li变色
            // this.colorId=res.data[0].news_id;//一上来选择第一个评论！
            // this.getComment(this.colorId);

          } else {
            console.log(res.msg);
          }
          if (res.status == "relogin") {
            this.$router.push("/login");
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    getComment(id) {
      this.$post(api.comment_list, {
        token: this.Token,
        page: this.commentPage,
        news_id: id
      })
        .then(res => {
          if (res.status == "0") {
            this.comment_table = res.data;
            // 获取分页数据
            this.commenTotals = res.total;
            this.commenPageTotal = res.pageTotal;
            this.commentPage = res.currPage;
    
          } else {
            // this.$message({ type: "info", message: res.msg });            
            this.comment_table=[];
          }
          if (res.status == "relogin") {
            this.$router.push("/login");
          }         
        })
        .catch(error => {
          console.log(error);
        });
    },
    search_key(select_word) {
      if (select_word == "") {
        this.$message.error(`搜索不能为空,请输入文章名称或者ID`);
        this.getArticle(this.select_word);
        return;
      } else {
        this.currPage = 1;
        this.getArticle(this.select_word);
      }
    },
    enterList(ID) {
      //点击列表
      this.colorId=ID;      
      this.getComment(ID);
      this.newsID=ID;
    },
    delate(index) {
      //删除  
      this.delVisible = true;
      this.commenID=index;
    },
    deleteRow() {
      // 确定删除   
      this.$post(api.comment_delete,{
        token:this.Token,
        comment_id:this.commenID,
        news_id:this.newsID
      }).
      then(res=>{
        if(res.status=="0"){
          this.$message.success("删除成功");
          this.getComment(this.newsID);
          this.getArticle();
        }
        else {
          this.$message.error(res.msg);         
          this.getArticle();
        }
        if (res.status == "relogin") {
          this.$message.error(res.msg);
          this.$router.push("/login");
        }
      }).catch(error=>{
        console.log(error);
      })
      this.delVisible = false;
    },
    //点击分页
    handleCurrentChange(val) {
      this.currPage = val;
      this.getArticle(this.select_word);
    },
    handComment(val){//列表分页
      this.currPage = val;
      this.getComment(this.newsID);
    }
  },
  created() {
    this.getArticle();
  }
};
</script>
<style scoped>
ul li {
  list-style: none;
}
.content {
  display: flex;
  justify-content: space-between;
}
.article_search {
  width: 350px;
  overflow: hidden;
  border: 1px solid #ccc;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 0 8px #888;
}
.article_search .title {
  padding: 10px;
  display: flex;
  justify-content: space-between;
}
.article_search .title .el-input {
  width: 240px;
}
.el-input__inner {
  height: 40px !important;
}
.comment_list_left {
  border-top: 1px solid #ccc;
}
.comment_list_left li {
  padding: 10px;
  border-bottom: 1px solid #ccc;
  cursor: pointer;
}
.comment_list_left li:last-child {
  border-bottom: none;
}

.comment_list_left .bdcolor{
  color: #fff;
  background: #409eff;
}
.comment_list_left .bdcolor h2 {
  color: #fff;
}
.comment_list_left li h2 {
  font-size: 16px;
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: #333;
  padding: 4px 0;
}
.comment_list_left li p {
  display: flex;
  padding-top: 4px;
  justify-content: space-between;
}
.comment_list_left li p span i {
  font-style: normal;
}
/* 分页 */
.Pagination_block {
  border-top: 1px solid #ccc;
  padding: 10px;
}
.Pagination_block .el-pagination__jump {
  margin-left: 10px !important;
}

/* 右边 */
.list_right {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  width: 1200px;
  overflow: hidden;
  border: 1px solid #ccc;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 0 8px #888;
}
.list_right .title {
  padding: 10px;
  height: 40px;
  line-height: 40px;
  border-bottom: 1px solid #ccc;
}
.list_right .title h2 {
  font-size: 20px;
}
.list_right .comment_table {
  flex: 8;
}
.comment_table ul li {
  padding: 10px;
  border-bottom: 1px solid #ccc;
}
.comment_table ul li h2 {
  font-size: 16px;
  padding-bottom: 10px;
  color: #000;
}
.comment_table ul li .desc {
  overflow: auto;
  width: 100%;
  height: 60px;
  color: #666;
}
.comment_table ul li p {
  padding-top: 10px;
  color: #000;
  font-size: 14px;
}
.comment_table ul li {
  display: flex;
}
.comment_table ul li .left {
  flex: 9;
}
.comment_table ul li .left p span {
  margin-right: 10px;
}
.comment_table ul li .right {
  flex: 1;
  line-height: 117px;
  text-align: center;
}

.comment_bottom {
  flex: 1;
  border-top: 1px solid #ccc;
  padding-top: 20px;
}
</style>
